import { Navigate, useLocation } from 'react-router-dom'

const Auth = ({ children }) => {
  const location = useLocation()
  const token = localStorage.getItem('token')

  if (!token && location.pathname !== '/login') {
    return <Navigate to={'/login'} replace={true} />
  }

  if (token && location.pathname === '/login') {
    return <Navigate to={'/'} replace={true} />
  }

  return children
}
export default Auth
